from applications.extensions import db
from datetime import datetime


class VoicePackage(db.Model):
    __tablename__ = 'voice_package'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='id')
    uid = db.Column(db.Integer, nullable=True, comment='用户id')
    name = db.Column(db.String(255), nullable=True, comment='语音包名称')
    avatar = db.Column(db.String(255), nullable=True, comment='语音包头像')
    intro = db.Column(db.String(255), nullable=True, comment='语音包简介')
    play_count = db.Column(db.String(255), nullable=True, comment='播放量')
    thump_count = db.Column(db.String(255), nullable=True, comment='点赞量')
    update_time = db.Column(db.DateTime, nullable=True, default=datetime.utcnow, onupdate=datetime.utcnow, comment='更新时间')
    create_time = db.Column(db.DateTime, nullable=True, default=datetime.utcnow, comment='创建时间')

    def to_dict(self):
        return {
            'id': self.id,
            'uid': self.uid,
            'name': self.name,
            'avatar': self.avatar,
            'intro': self.intro,
            'playCount': self.play_count,
            'thumpCount': self.thump_count,
            'updateTime': self.update_time.isoformat() if self.update_time else None,
            'createTime': self.create_time.isoformat() if self.create_time else None
        }